package cn.stormling.note5.bio.thread;

import cn.stormling.note5.bio.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class HelloServer {
    private static final Logger logger = LoggerFactory.getLogger(HelloServer.class);
    /**
     * 线程池解决方法
     * @param port
     */
    public void start(int port) {
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ExecutorService threadPool = new ThreadPoolExecutor(
                10, 100, 1,
                TimeUnit.MINUTES, new ArrayBlockingQueue<>(100), threadFactory);
        threadPool.execute(() -> {
            // 创建 socket 连接
            //1.创建 ServerSocket 对象并且绑定一个端口
            try (ServerSocket server = new ServerSocket(port);) {
                Socket socket;
                //2.通过 accept()方法监听客户端请求， 这个方法会一直阻塞到有一个连接建立
                while ((socket = server.accept()) != null) {
                    logger.info("client connected");
                    try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                         ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream())) {
                        //3.通过输入流读取客户端发送的请求信息
                        Message message = (Message) objectInputStream.readObject();
                        logger.info("server receive message:" + message.getContent());
                        message.setContent("new content");
                        //4.通过输出流向客户端发送响应信息
                        objectOutputStream.writeObject(message);
                        objectOutputStream.flush();
                    } catch (IOException | ClassNotFoundException e) {
                        logger.error("occur exception:", e);
                    }
                }
            } catch (IOException e) {
                logger.error("occur IOException:", e);
            } finally {

            }
        });
        /**
         * 多线程解决---避免不了资源消耗问题
         */
        new Thread(() -> {

        }).start();
    }

    public static void main(String[] args) {
        HelloServer helloServer = new HelloServer();
        helloServer.start(6666);
    }
}